package com.zl.tby.core.base.util;

//import org.bytedeco.javacv.FFmpegFrameGrabber;
//import org.bytedeco.javacv.Frame;
//import org.bytedeco.javacv.Java2DFrameConverter;
//import javax.imageio.ImageIO;
//import java.awt.image.BufferedImage;
import java.io.*;
//import java.net.URL;
//import java.util.ArrayList;
//import java.util.List;

public class VideoToImageUtil {

    public static boolean videoToImage(InputStream videoInputStream, String imageName) {
        return false;
    }

    /*public static void main(String[] args) throws Exception {
        List<String> dataList = videoToImage("D:/aaa/1111.mp4", "D:/aaa", new Double[]{1D, 10D, 5D});
        for (String s : dataList) {
            System.out.println(s);
        }
        URL url = new URL("http://175.44.137.60:12007/file/img/2024/11/4/15/QXfmkqIEbD5M-Y5fJ2_68.mp4");
        InputStream is = url.openStream();
        System.out.println(videoToImage(is, "D:/aaa/1.jpg"));
    }

    *//**
     * 视频转图片
     * @param videoPath：视频地址
     * @param outputDir：文件存储路径
     * @param timesInSeconds: 在第几秒进行截取
     * @return 图片存储路径列表
     *//*
    public static List<String> videoToImage(String videoPath, String outputDir, Double[] timesInSeconds) {
        // 使用 File 类来提取文件名
        File videoFile = new File(videoPath);
        // 获取文件名（包含扩展名
        String videoName = videoFile.getName();
        // 去掉扩展名
        int dotIndex = videoName.lastIndexOf('.');
        if (dotIndex > 0) {
            videoName = videoName.substring(0, dotIndex);
        }
        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(videoPath);
        Java2DFrameConverter converter = new Java2DFrameConverter();
        // 返回接入图片路径
        List<String> filePathArrays = new ArrayList();
        try {
            // 开始读取视频
            frameGrabber.start();
            // 获取视频的帧率
            double frameRate = frameGrabber.getFrameRate();
            for (double timeInSeconds : timesInSeconds) {
                int frameNumberToCapture = (int) (timeInSeconds * frameRate);
                // 设置到指定的帧
                frameGrabber.setFrameNumber(frameNumberToCapture);
                // 仅抓取图像帧
                Frame frame = frameGrabber.grabImage();
                if (frame != null) {
                    String imagePath = outputDir + File.separator + videoName + "_" + timeInSeconds + "s.jpg";
                    // 保存图片
                    ImageIO.write(converter.convert(frame), "jpg", new File(imagePath));
                    filePathArrays.add(imagePath);
                }
            }
            // 停止读取视频
            frameGrabber.stop();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                frameGrabber.release();
            } catch (FFmpegFrameGrabber.Exception e) {
                e.printStackTrace();
            }
            return filePathArrays;
        }
    }

    *//**
     * 视频转图片
     * @param videoInputStream: 视频流
     * @param imageName：输出图片
     * @return true： 成功， false： 失败
     *//*
    public static boolean videoToImage(InputStream videoInputStream, String imageName) {
        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(videoInputStream);
        Java2DFrameConverter converter = new Java2DFrameConverter();
        try {
            // 开始读取视频
            frameGrabber.start();
            frameGrabber.setFrameNumber(0);
            // 仅抓取图像帧
            Frame frame = frameGrabber.grabImage();
            if (frame != null) {
                ImageIO.write(converter.convert(frame), "jpg", new File(imageName));
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                frameGrabber.stop();
                // 释放资源
                frameGrabber.release();
                videoInputStream.close();
            } catch (FFmpegFrameGrabber.Exception e) {
                e.printStackTrace();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return false;
    }

    *//**
     * 将frame对象转换成文件流
     * @param frame: 对象
     * @return 文件流
     *//*
    private static InputStream getInputStream(Frame frame){
        // 使用Java2DFrameConverter将Frame转换为BufferedImage
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bufferedImage = converter.convert(frame);
        // 将BufferedImage转换为byte[]
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "jpg", bao);
            // 使用byte[]创建一个ByteArrayInputStream
            return new ByteArrayInputStream(bao.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }*/
}
